﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Checkout : System.Web.UI.Page
{
    List<int> removeList = new List<int>();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        MyCart cart = (MyCart)Session["cart"];
        if (!IsPostBack)
        {
            if (cart.GetNumberofItem() == 0)
            {
                Label1.Text = "Bạn không có món hàng nào trong giỏ!";
            }
            else
            {
                lblAmount.Text = cart.GetTotalPrice().ToString() + " VNĐ";
            }
            if (Session["UserName"] != null)
            {
                String username = Session["UserName"].ToString();
                loadCustomer(username);
            }
            Label1.Text = "";
        }
    }
    private void loadCustomer(String username)
    {
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        var customer = cdt.Select_CustomerByUsername(username).First();
        txtAddress.Text = customer.Address;
        txtCardNumber.Text = customer.Bank_Account_ID.ToString();
        txtEmail.Text = customer.Email;
        txtFirstName.Text = customer.FullName;
    }
    private void RemoveFromList()
    {
        MyCart cart = (MyCart)Session["Cart"];
        if (removeList.Count > 0)
        {
            for (int i = 0; i < cart.Items.Count; i++)
            {
                for (int j = 0; j < removeList.Count; j++)
                {
                    if (cart.Items[i].ProductID == removeList[j])
                    {
                        cart.RemoveItem(removeList[j]);
                    }
                }
            }

        }
    }
    
    protected void btnCash_Click(object sender, EventArgs e)
    {
        MyCart cart = (MyCart)Session["cart"];
        if (Session["cart"] == null || Session["UserName"] == null)
        {
            return;
        }
        String username = Session["UserName"].ToString();
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        var customer = cdt.Select_CustomerByUsername(username).First();
        int customerID = customer.ID;
        float total = (float)cart.GetTotalPrice();

        int? orderID = -1; // get id inserted
        int temp = 1;
        //cdt.Insert_Order(customerID, 1, total, ref orderID);
        //cdt.SubmitChanges();
        cdt.Insert_Order( customerID, temp, total,ref orderID);
        cdt.SubmitChanges();

        Label1.Text = orderID.ToString();

        ChoDienTuDataContext cdt1 = new ChoDienTuDataContext();
        foreach(CartItem p in cart.Items)
        {
            cdt1.Insert_OrderDetail(orderID, p.ProductID, p.Quantity);
            cdt1.SubmitChanges();
           // cart.RemoveItem(p.ProductID);
        }

        Bank2DataContext bank = new Bank2DataContext();
        bank.Tranfer(customer.Bank_Account_ID, 1, total);
        bank.SubmitChanges();

        // update product quantity
        foreach (CartItem p in cart.Items)
        {
            cdt1.Update_ProductQuantity(p.ProductID, p.Quantity);
            cdt1.SubmitChanges();
        }


        Label1.Text ="Mua hàng thành công OrderID: "+orderID.ToString();
        Session["cart"] = null;
        cart = new MyCart();
        ASP.masterpage_master master = (ASP.masterpage_master)this.Master;
        master.RefreshShoppingCartInfo();
        Response.Redirect("UserOrder.aspx?id=" + orderID.ToString());
            
    }
}